package com.base.caselib;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.caselib.bean.CheckRecordBean;
import com.base.caselib.bean.PaintEvent;
import com.base.caselib.bean.PdfType;
import com.base.caselib.databinding.ActivityCheckrecordBinding;
import com.base.caselib.presenter.CheckRecordPresenter;
import com.base.caselib.view.CheckRecordView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.CasePrint;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.TimePickUtil;
import com.base.commonlib.widget.SingleChooseDialog;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 制作通知书及现场检查笔录
 * Created by weikailiang on 2019/10/29.
 */
@Route(path = "/checkrecord/check")
public class CheckRecordActivity extends CBaseBarMvpActivity<CheckRecordPresenter> implements CheckRecordView{

    private ActivityCheckrecordBinding mBinding;
    private ArrayList<PdfType> mPdfTypes=new ArrayList<>();
    private SimpleDateFormat dateFormatNYRHFM = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    String mFormId;
    @Autowired
    String mCaseId;
    @Autowired
    String mCaseAbstract;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_checkrecord;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityCheckrecordBinding) mRootBinding;
        mBinding.setEntity(new CasePrint());
        mPresenter = new CheckRecordPresenter(this,this);
        mPresenter.start();
        mPresenter.initPics(mFormId,mCaseId);
        setBackNavigation();
        setBarTitle("一般案件");
        setRigImg(R.drawable.ic_print, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!mPresenter.mHasSaved)
                {
                    showMes("请先保存");
                    return;
                }
                new SingleChooseDialog<PdfType>(mContext, "打印", mPdfTypes, new SingleChooseDialog.OnItemChooseListener() {
                    @Override
                    public void onItemChoose(Object o) {
                        PdfType type = (PdfType) o;
                        switch (type.getValue()){
                            case "CaseAllDocument":
                                break;
                            case "OrderStopInform":
                                mPresenter.getNoticePdfUrl(mCaseId,type.getType());
                                break;
                            case "FieldInspectionRecord":
                                mPresenter.getPdfUrl(mCaseId,type.getType());
                                break;
                            case "zenlingzhengg":
                                mPresenter.getPdfUrlZLZG(mCaseId,type.getType());
                                break;
                            case "dizhiquerenshu":
                                mPresenter.getPdfUrlAddress(mCaseId,type.getType());
                                break;
                        }
                    }
                }).show();
            }
        });
//        mPdfTypes.add(new PdfType("全部", "CaseAllDocument"));
        mPdfTypes.add(new PdfType("通知书", "OrderStopInform"));
        mPdfTypes.add(new PdfType("现场检查笔录", "FieldInspectionRecord"));
        mPdfTypes.add(new PdfType("责令停止违反城市管理行为通知书","zenlingzhengg"));
        mPdfTypes.add(new PdfType("地址确认书","dizhiquerenshu"));
        Disposable disposable = RxBus2.getInstance().doSubscribe(PaintEvent.class, new Consumer<PaintEvent>() {
            @Override
            public void accept(PaintEvent mPaint) throws Exception {
                if(!TextUtils.isEmpty(mPaint.getPath()))
                {
                    mPresenter.mcasePrint.setPath(mPaint.getPath());
                    GlideUtil.loadFile(mContext,mPaint.getPath(),mBinding.ivRoad);
                }else if(!TextUtils.isEmpty(mPaint.getPath()))
                {
                    mPresenter.mcasePrint.setPath(mPaint.getPath());
                    GlideUtil.load_nocatch(mContext,mPaint.getPath(),mBinding.ivRoad);
                }
                if (!TextUtils.isEmpty(mPaint.getRoad1())){
                    mPresenter.fieldInspectionRoad1 = mPaint.getRoad1();
                }
                if (!TextUtils.isEmpty(mPaint.getRoad2())){
                    mPresenter.fieldInspectionRoad2 = mPaint.getRoad2();
                }
                if (!TextUtils.isEmpty(mPaint.getRefrence1())){
                    mPresenter.fieldInspectionReference1 = mPaint.getRefrence1();
                }
                if (!TextUtils.isEmpty(mPaint.getRefrence2())){
                    mPresenter.fieldInspectionReference2 = mPaint.getRefrence2();
                }

            }
        });
        RxBus2.getInstance().addSubscription(this, disposable);

        mBinding.btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> list = new ArrayList<>();
                if (TextUtils.isEmpty(mPresenter.mcasePrint.getPath())){
                    showMes("请添加示意图");
                    return;
                }
                if (mPresenter.mcasePrint.getPath().startsWith("http")){
                    //无修改照片
                }else {
                    list.add(mPresenter.mcasePrint.getPath());
                }
                mPresenter.upRandom(mCaseId,mFormId,mBinding.getEntity().getFact(),mBinding.getEntity().getSendDate(),list);
            }
        });
    }

    @Override
    protected void start() {
        mBinding.ivMake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/paint/paint").navigation();
            }
        });
        mBinding.ivDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TimePickUtil.pickDateTime(mContext, mBinding.tvDate);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.get(this).clearMemory();
        mPresenter.release();
    }

    @Override
    public void returnCheckRecordBean(CheckRecordBean bean) {
        if (bean.getFieldRecord()!=null||TextUtils.isEmpty(bean.getFieldRecord())){
            mPresenter.mHasSaved = true;
        }else {
            mPresenter.mHasSaved = false;
        }
        if (bean.getInvestigationDate()==null){
            mPresenter.mcasePrint.setSendDate(dateFormatNYRHFM.format(new Date()));
        }else {
            mPresenter.mcasePrint.setSendDate(bean.getInvestigationDate());
        }
        mPresenter.mcasePrint.setFact(bean.getFieldRecord());
        if (bean.getSketchPicList()!=null&&bean.getSketchPicList().size()>0) {
            mPresenter.mHasSaved = true;
            List<CheckRecordBean.fileBean> fileBean1 = new Gson().fromJson(bean.getSketchPicList().get(0),new TypeToken<List<CheckRecordBean.fileBean>>(){}.getType());
            for (CheckRecordBean.fileBean fileBean : fileBean1) {
                if (fileBean.getContent().startsWith("http")) {
                    mPresenter.mcasePrint.setPath(fileBean.getContent());
                    GlideUtil.load(mContext, fileBean.getContent(), mBinding.ivRoad);
                } else {
                    mPresenter.mcasePrint.setPath(RealNetWorkUtil.getBaseUrl() + "api-file/" + fileBean.getContent());
                    GlideUtil.load(mContext, RealNetWorkUtil.getBaseUrl() + "api-file/" + fileBean.getContent(), mBinding.ivRoad);
                }
            }
        }else {
            mPresenter.mHasSaved = false;
        }
        if (TextUtils.isEmpty(bean.getFieldRecord())){
            mPresenter.mcasePrint.setFact(mCaseAbstract);
        }else {
            mPresenter.mcasePrint.setFact(bean.getFieldRecord());
        }
        mBinding.setEntity(mPresenter.mcasePrint);
    }
}
